package com.freework.sys.table.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import com.freework.sys.cache.CacheProviderManager;
import com.freework.sys.cache.DICGroupCollection;
import com.freework.sys.dic.service.TableDataManagerFactory;
import com.freework.sys.permissions.external.exception.AppException;
import com.freework.sys.table.dao.SysDicTableDAO;
import com.freework.sys.table.dao.SysDiccolumnDAO;
import com.freework.sys.table.vo.SysDiccolumnVO;
import com.freework.sys.table.vo.SysDictableVO;

import constant.CacheKeyName;
public class TableServiceImpi implements TableServiceI {
	@Resource()
	SysDicTableDAO dao=null;
	@Resource()
	SysDiccolumnDAO columnDao=null;
	TableDataManagerFactory predefineTableFactory=null;
	TableDataManagerFactory dicVOCfigFactory=null;
	
	@Resource(name="dic.predefineTable.TableAndClass")
	Map<String,Class> tableAndClass=null;
	
	public void setTableAndClass(Map<String, Class> tableAndClass) {
		this.tableAndClass = tableAndClass;
	}


	public void setPredefineTableFactory(
			TableDataManagerFactory predefineTableFactory) {
		this.predefineTableFactory = predefineTableFactory;
	}


	public void setDicVOCfigFactory(TableDataManagerFactory dicVOCfigFactory) {
		this.dicVOCfigFactory = dicVOCfigFactory;
	}


	public void setColumnDao(SysDiccolumnDAO columnDao) {
		this.columnDao = columnDao;
	}


	public Map<String,SysDictableVO> findTableAll(){
		 HashMap<String,SysDictableVO> map=CacheProviderManager.get(HashMap.class,"dic_table_info_cache", CacheKeyName.TABLE_INFO_NAME);
			if(map==null){
				SysDictableVO vo=new SysDictableVO();
				List<SysDictableVO> list=dao.query(vo);
				map=new HashMap<String,SysDictableVO>(list.size());
				for (SysDictableVO sysDictableVO : list) {
					TableDataManagerFactory factory=getTableDataManagerFactory(sysDictableVO);
					sysDictableVO.setDicmanager(factory.createDicVo(sysDictableVO, findColumnAllByTableName(sysDictableVO.getTablename())));
					map.put(sysDictableVO.getTablename(), sysDictableVO);
				}
				CacheProviderManager.set("dic_table_info_cache", CacheKeyName.TABLE_INFO_NAME, map);
				return map;
			}else
				return map;
			
		}
	DICGroupCollection<SysDiccolumnVO> findColumnAllGroup(){
		DICGroupCollection<SysDiccolumnVO> collection=CacheProviderManager.get(DICGroupCollection.class,"dic_table_info_cache", CacheKeyName.TABLE_INFO_COL);
		if(collection==null){
			collection=columnDao.findColumnAll();
			CacheProviderManager.set("dic_table_info_cache", CacheKeyName.TABLE_INFO_COL, collection);
			return collection;
		}else
			return collection;
		
		
	}
		
	public Collection<SysDiccolumnVO> findColumnAll(){
		return findColumnAllGroup().getAll();
	}
	public List<SysDiccolumnVO> findColumnAllByTableName(String tablename){
		return findColumnAllGroup().getDICByGroupcode(tablename);
	}
	
	public SysDiccolumnVO findColumn(String tablename,String columnname){
		return findColumnAllGroup().getDICByCode(tablename, columnname);
	}
	
	
	public SysDictableVO findTableByName(String tablename){
			Map<String,SysDictableVO> map=findTableAll();
			return map.get(tablename);
			
	}

	private TableDataManagerFactory getTableDataManagerFactory(SysDictableVO vo){
		
		if((vo.getPredefine()!=null&&vo.getPredefine().intValue()==1)||tableAndClass.containsKey(vo.getTablename())){
				return predefineTableFactory;
		}else{
				return dicVOCfigFactory;
			
		}

		
	}
	public void clearTableCache() throws AppException {
		CacheProviderManager.evict("dic_table_info_cache", CacheKeyName.TABLE_INFO_NAME);
		CacheProviderManager.evict("dic_table_info_cache", CacheKeyName.TABLE_INFO_COL);
		CacheProviderManager.evict("dic_table_info_cache", CacheKeyName.TABLE_PAGE);
		CacheProviderManager.clear("dic_cache");
	}
}
